package com.app.shop.service.system.impl;

import com.app.shop.util.SysModel;
import com.app.shop.dao.system.SysReAuthMenuMapper;
import com.app.shop.entity.SysReAuthMenu;
import com.app.shop.entity.SysReAuthMenuExample;
import com.app.shop.entity.query.SysAuthQuery;
import com.app.shop.service.system.SysReAuthMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 权限服务
 *
 * @author chenliwei
 * @version v1.0 2019-3-11
 */
@Service("sysReAuthMenuService")
public class SysReAuthMenuServiceImp implements SysReAuthMenuService {

    private static final Logger log = LoggerFactory.getLogger(SysReAuthMenuServiceImp.class);

    @Autowired
    private SysReAuthMenuMapper sysReAuthMenuMapper;


    @Override
    public List<SysReAuthMenu> getSysMenuListByAuthId(Integer authId) {
        SysReAuthMenuExample sysReAuthMenuExample = new SysReAuthMenuExample();
        SysReAuthMenuExample.Criteria criteria = sysReAuthMenuExample.createCriteria();
        criteria.andStatusEqualTo(1).andAuthIdEqualTo(authId);
        return sysReAuthMenuMapper.selectByExample(sysReAuthMenuExample);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysModel updateAuthMenuInfo(SysAuthQuery sysAuthQuery) {
        SysModel sysModel = new SysModel();
        try {
            if(!sysAuthQuery.getAuthId().equals(1001)) {
                SysReAuthMenuExample sysReAuthMenuExample = new SysReAuthMenuExample();
                SysReAuthMenuExample.Criteria criteria = sysReAuthMenuExample.createCriteria();
                criteria.andAuthIdEqualTo(sysAuthQuery.getAuthId()).andStatusEqualTo(1);
                SysReAuthMenu SysReAuthMenu = new SysReAuthMenu();
                SysReAuthMenu.setVersion(new Date());
                SysReAuthMenu.setStatus(-1);
                SysReAuthMenu.setUpdateUserId(sysAuthQuery.getOptionUserId());

                if (sysAuthQuery.getMenuIds() != null && sysAuthQuery.getMenuIds().length > 0) {
                    List<Integer> menuList = new ArrayList<Integer>();
                    menuList.addAll(Arrays.asList(sysAuthQuery.getMenuIds()));
                    criteria.andMenuIdNotIn(menuList);
                    //将当前用户权限失效
                    sysReAuthMenuMapper.updateByExampleSelective(SysReAuthMenu, sysReAuthMenuExample);
                    //查询已经权限
                    sysReAuthMenuExample = new SysReAuthMenuExample();
                    SysReAuthMenuExample.Criteria criteriaIn = sysReAuthMenuExample.createCriteria();
                    criteriaIn.andAuthIdEqualTo(sysAuthQuery.getAuthId()).andAuthIdIn(menuList).andStatusEqualTo(1);
                    List<SysReAuthMenu> list = sysReAuthMenuMapper.selectByExample(sysReAuthMenuExample);
                    //系统已经存在权限不进行循环插入
                    boolean bool;
                    //开始循环添加权限
                    for (Integer it : sysAuthQuery.getMenuIds()) {
                        bool = true;
                        for (SysReAuthMenu srau : list) {
                            if (srau.getMenuId().equals(it)) {
                                bool = false;
                                break;
                            }
                        }
                        if (bool) {
                            //增加生效规则，增加批量插入sql
                            SysReAuthMenu = new SysReAuthMenu();
                            SysReAuthMenu.setCreateDate(new Date());
                            SysReAuthMenu.setStatus(1);
                            SysReAuthMenu.setCreateUserId(sysAuthQuery.getOptionUserId());
                            SysReAuthMenu.setVersion(new Date());
                            SysReAuthMenu.setAuthId(sysAuthQuery.getAuthId());
                            SysReAuthMenu.setMenuId(it);
                            SysReAuthMenu.setUpdateUserId(sysAuthQuery.getOptionUserId());
                            sysReAuthMenuMapper.insert(SysReAuthMenu);
                        }
                    }
                } else {
                    //将当前用户权限失效
                    sysReAuthMenuMapper.updateByExampleSelective(SysReAuthMenu, sysReAuthMenuExample);
                }
                sysModel.setSuccess(true);
            }else{
                sysModel.setSuccess(false);
                sysModel.setMsg("超级管理员不允许变更角色！");
            }
        } catch (Exception e) {
            sysModel.setSuccess(false);
            sysModel.setMsg("权限菜单修改过程发生异常,请重试或联系管理员！");
            log.error("权限菜单修改过程发生异常：" + e);
        }
        
        return sysModel;
    }

    @Override
    public List<SysReAuthMenu> getSysMenuListByAuthId(List<Integer> authIds) {
        SysReAuthMenuExample sysReAuthMenuExample = new SysReAuthMenuExample();
        SysReAuthMenuExample.Criteria criteria = sysReAuthMenuExample.createCriteria();
        criteria.andStatusEqualTo(1).andAuthIdIn(authIds);
        return sysReAuthMenuMapper.selectByExample(sysReAuthMenuExample);    }
}
